package thread;
class MyBLOCKEDQUEUE{

    private String elems[]=null;//队列存储结构
    private int size=0;//当前元素数量
    private int tail=0;//队尾
    private int head=0;//队头
    private Object locker=new Object();
    public MyBLOCKEDQUEUE(int size){

        this.elems=new String[size];
    }
    public void put(String elem) throws InterruptedException {
        synchronized (locker) {
            while (size>= elems.length){
                locker.wait();
            }
            elems[tail] = elem;
            size++;
            tail++;
            if (tail >= elems.length) {
                tail = 0;
            }
        }
        locker.notify();
    }
    public String take() throws InterruptedException {
        synchronized (locker) {
             if (size==0){
                locker.wait();
            }
            String ret = elems[head];
            head++;
            size--;
            if (head >= elems.length) {
                head = 0;
            }
            locker.notify();
            return ret;
        }
    }
}
public class ThreadDemo26 {
}
